package cn.admin.configcenter.configservice.domain;

import cn.admin.configcenter.configservice.util.MD5;

import java.io.Serializable;

public class ConfigInfo implements Serializable, Comparable<ConfigInfo> {

	private long id;
	private String dataId;
	private String group;
	private String content;
	private String md5;
	private String appName;
	private String srcIp;
	private long createTimestamp;
	private long lastModifiedTimestamp;

	@Override
	public int hashCode() {
		int result = 1;
		result = 31 * result + (this.content == null ? 0 : this.content.hashCode());
		result = 31 * result + (this.dataId == null ? 0 : this.dataId.hashCode());
		result = 31 * result + (this.group == null ? 0 : this.group.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		} else if (obj == null) {
			return false;
		} else if (this.getClass() != obj.getClass()) {
			return false;
		} else {
			ConfigInfo other = (ConfigInfo) obj;
			if (this.content == null) {
				if (other.content != null) {
					return false;
				}
			} else if (!this.content.equals(other.content)) {
				return false;
			}

			if (this.dataId == null) {
				if (other.dataId != null) {
					return false;
				}
			} else if (!this.dataId.equals(other.dataId)) {
				return false;
			}

			if (this.group == null) {
				if (other.group != null) {
					return false;
				}
			} else if (!this.group.equals(other.group)) {
				return false;
			}

			return true;
		}
	}

	@Override
	public String toString() {
		return "ConfigInfo{" +
				"id=" + id +
				", dataId='" + dataId + '\'' +
				", group='" + group + '\'' +
				", content='" + content + '\'' +
				", md5='" + md5 + '\'' +
				", appName='" + appName + '\'' +
				", srcIp='" + srcIp + '\'' +
				", createTimestamp=" + createTimestamp +
				", lastModifiedTimestamp=" + lastModifiedTimestamp +
				'}';
	}

	public ConfigInfo() {
	}

	public ConfigInfo(String dataId, String group) {
		this.dataId = dataId;
		this.group = group;
		this.content = "";
		this.md5 = MD5.getInstance().getMD5String(this.content);
	}

	public ConfigInfo(String dataId, String group, String content) {
		this.dataId = dataId;
		this.group = group;
		this.content = content;
		this.md5 = MD5.getInstance().getMD5String(content);
	}

	public ConfigInfo(String dataId, String groupId, String content, String srcIp, String appName) {
		this.dataId = dataId;
		this.group = groupId;
		this.content = content;
		this.md5 = MD5.getInstance().getMD5String(content);
		this.srcIp = srcIp;
		this.appName = appName;
	}

	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public String getDataId() {
		return dataId;
	}

	public void setDataId(String dataId) {
		this.dataId = dataId;
	}

	public String getGroup() {
		return group;
	}

	public void setGroup(String group) {
		this.group = group;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public String getMd5() {
		return md5;
	}

	public void setMd5(String md5) {
		this.md5 = md5;
	}

	public String getAppName() {
		return appName;
	}

	public void setAppName(String appName) {
		this.appName = appName;
	}

	public String getSrcIp() {
		return srcIp;
	}

	public void setSrcIp(String srcIp) {
		this.srcIp = srcIp;
	}

	public long getCreateTimestamp() {
		return createTimestamp;
	}

	public void setCreateTimestamp(long createTimestamp) {
		this.createTimestamp = createTimestamp;
	}

	public long getLastModifiedTimestamp() {
		return lastModifiedTimestamp;
	}

	public void setLastModifiedTimestamp(long lastModifiedTimestamp) {
		this.lastModifiedTimestamp = lastModifiedTimestamp;
	}

	@Override
	public int compareTo(ConfigInfo o) {
		if (o == null) {
			return 1;
		} else if (this.dataId == null && o.getDataId() != null) {
			return -1;
		} else {
			int cmpDataId = this.dataId.compareTo(o.getDataId());
			if (cmpDataId != 0) {
				return cmpDataId;
			} else if (this.group == null && o.getGroup() != null) {
				return -1;
			} else {
				int cmpGroup = this.group.compareTo(o.getGroup());
				if (cmpGroup != 0) {
					return cmpGroup;
				} else if (this.content == null && o.getContent() != null) {
					return -1;
				} else {
					int cmpContent = this.content.compareTo(o.getContent());
					return cmpContent;
				}
			}
		}
	}
}
